<?php  
/*
$Id$
*/


if (!defined('BASEPATH')) exit('No direct script access allowed');

class Hevitra extends Controller {
	var $settings = array();
	function Hevitra()
	{
		parent::Controller();
		$this->template['module'] = "album";
		$this->load->model("album_model", "album");
		//$this->load->helper("bbcode");
	}
	
	function ampidiro($id = null)
	{
		$this->user->require_login();
		if ($this->input->post('submit') && $this->input->post('comment') && $this->input->post('sid'))
		{
			$this->album->save_comment();
			$this->plugin->do_action('album_save_comment', $this->input->post('sid'));
			
			$row = $this->album->get_image(array('where' => array('sid' => $this->input->post('sid'))));
			
			$notify_image_owner = true;
			$notify_image_owner = $this->plugin->apply_filters('album_comment_notify_owner', $notify_image_owner, $row['username']);
			
			if ($row['email'] && $notify_image_owner)
			{
				$this->load->library('email');

				$this->email->from($this->system->admin_email, $this->system->site_name );
				$this->email->to($row['email']);

				$this->email->subject('[' . $this->system->site_name . '] '. __("Comment notification", $this->template['module']));
			
			$smsg = __("
Hello,

A comment has been added to the photo you submitted

---
Submitted by: %s
on date: %s

%s

Click on the link below to reply

%s
---

", "album");
			$msg = sprintf($smsg, $this->user->username, 
					date("d/m/Y"), strip_tags($this->input->post('comment')), site_url('album/sary/asehoy/' . $row['sid'])
				);
				
			$this->email->message($msg);

			$this->email->send();
			
			}
			
			//notify others
			
			$params = array('where' => array('album_images.sid' => $row['sid'], 'album_image_comments.notify' => 1, 'album_image_comments.username <>' => $row['username'], 'album_image_comments.username <>' => $this->user->username));
			$aready = array();
			if($emails = $this->album->get_comments($params))
			{
				$this->load->library('email');
				foreach($emails as $email)
				{
					if(isset($already[$email['email']])) continue;
					$already[$email['email']] = 1;
					$this->email->from($this->system->admin_email, $this->system->site_name );
					$this->email->to($email['email']);

					$this->email->subject('[' . $this->system->site_name . '] '. __("Comment notification", $this->template['module']));
				
					$smsg = __("
Hello,

Someone replied to your comment

---
Submitted by: %s
on date: %s

%s

Click on the link below to reply

%s
---

", "album");
					$msg = sprintf($smsg, $this->user->username, 
						date("d/m/Y"), strip_tags($this->input->post('comment')), site_url('album/sary/asehoy/' . $email['sid'])
					);
					
					$this->email->message($msg);

					$this->email->send();
								
				}
			
			}
			
			redirect('album/sary/asehoy/' . $row['sid'], 'refresh');
			return;
		}
	
	
	}
	
	
	function vakio($sid = null, $start = 0)
	{
		$per_page = 10;
		if (is_null($sid))
		{
			$this->template['title'] = __("Comment error", "album");
			$this->template['message'] = __("You did not choose a photo.", "album");
			$this->layout->load($this->template, 'error');
			return;
			
		}
		
		$params = array(
			'where' => array('album_images.sid' => $sid),
			'limit' => $per_page,
			'start' => $start
		);
		if ($photo = $this->album->get_image(array('where' => array('album_images.sid' => $sid))))
		{
			if ($this->template['rows'] = $this->album->get_comments($params))
			{
					

				$config['uri_segment'] = 5;
				$config['first_link'] = __('First', 'album');
				$config['last_link'] = __('Last', 'album');
				$config['base_url'] = base_url() . 'album/hevitra/vakio/' . $sid ;
				$config['total_rows'] = $this->album->get_total_comments($params);
				$config['per_page'] = $per_page; 

				$this->load->library('pagination');
				
				$this->pagination->initialize($config); 

				$this->template['pager'] = $this->pagination->create_links();		
				$this->template['start'] = $start;		
				$this->template['total'] = $config['total_rows'];		
				$this->template['per_page'] = $config['per_page'];		
			
			
			
				$this->template['title'] = sprintf(__("Comments about the image inserted by %s", "album"), $photo['username']);
				$this->template['photo'] = $photo;
				$this->layout->load($this->template, 'photo/comments');
			}
			else
			{
				$this->template['title'] = __("Comment error", "album");
				$this->template['message'] = __("No comment found", "album");
				$this->layout->load($this->template, 'error');
			}
				
		}
		else
		{
			$this->template['title'] = __("Comment error", "album");
			$this->template['message'] = __("Album not found", "album");
			$this->layout->load($this->template, 'error');
		}
		
	}
	
	function delete($id = null, $js = 0)
	{
		if(is_null($id) || $id == 0)
		{
			echo "Erreur tsisy id";
			return;
		}	
		
		
		if($comment = $this->album->get_comment(array('where' => array('id' => $id))))
		{
			$image = $this->album->get_image(array('where' => array('album_images.sid' => $comment['sid'])));
			
			if($comment['username'] != $this->user->username && $this->user->level['album'] < LEVEL_DEL && $image['username'] != $this->user->username)
			{
				$this->template['title'] = __("Comment error", "album");
				$this->template['message'] = __("You cannot delete this comment", "album");
				$this->layout->load($this->template, 'error');
				return;
			}
			
			if($js == 0)
			{
				$this->template['title'] = __("Delete comment", "album");
				$this->template['id'] = $id;
				$this->layout->load($this->template, 'delete_comment');
			}
			else
			{
				$this->album->delete_comment(array('where' => array('id' => $id)));
				redirect('album/sary/asehoy/' . $comment['sid']);
			}
		}
		
	}
	
	
}
